﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.IO;
using Quartz.Collection;

namespace ihongma.test
{
    public partial class WebForm2 : System.Web.UI.Page
    {
        //Random seed = new Random();
        public Bitmap bmpobj;
        protected void Page_Load(object sender, EventArgs e)
        {
            bmpobj = new Bitmap(Server.MapPath("~/upload/m_1363239818050.jpg"));
             //GrayByPixels();
             //Response.Write(isBlackWhite(Server.MapPath("~/upload/图片11.png")));
          //  Response.Write(DateTime.Now.AddHours(-seed.Next(12)).ToLongTimeString());
           // Response.Write(DateTime.Parse("2013/06/14 " + DateTime.Now.AddHours(-seed.Next(12)).ToLongTimeString()));
            //var v = CountImageColors(Server.MapPath("~/upload/a.jpg")).Distinct(new ProductNoComparer()).ToList();
            //var vv = CountImageColors(Server.MapPath("~/upload/a.jpg"));
            ////Response.Write(v);
            //foreach (var i in v)
            //{
            //    Response.Write(i.);
            //}

            //   Response.Write(v.Count());
            //   Response.Write("</br>");
            //   Response.Write(vv.Count());
           //}
          // Response.Write(getDominantColor(bmpobj).Name);
         // (Color.Bisque) ==( Color.Bisque);
            //Response.Write(CountImageColors(Server.MapPath("~/upload/图片11.png")) + "</br>");
            //Response.Write(CountImageColors(Server.MapPath("~/upload/m_1363239818050.jpg")) + "</br>");
            //Response.Write(CountImageColors(Server.MapPath("~/upload/a.jpg")) + "</br>");
            //Response.Write(CountImageColors(Server.MapPath("~/upload/z.jpg")) + "</br>");
            //Response.Write(CountImageColors(Server.MapPath("~/upload/dd2.png"))+ "</br>");
            //Response.Write(CountImageColors(Server.MapPath("~/upload/Bond-Cars1.jpg")) + "</br>");
            //Response.Write(CountImageColors(Server.MapPath("~/upload/d.jpg"))+ "</br>");
            Response.Write(CountImageColors(Server.MapPath("~/upload/1.jpg")) + "</br>");
            Response.Write(CountImageColors(Server.MapPath("~/upload/2.jpg")) + "</br>");
            Response.Write(CountImageColors(Server.MapPath("~/upload/3.jpg")) + "</br>");
            Response.Write(CountImageColors(Server.MapPath("~/upload/4.jpg")) + "</br>");
        }
        /// <summary>
        /// 根据RGB，计算灰度值
        /// </summary>
        /// <param name="posClr">Color值</param>
        /// <returns>灰度值，整型</returns>
        private int GetGrayNumColor(System.Drawing.Color posClr)
        {
            return (posClr.R * 19595 + posClr.G * 38469 + posClr.B * 7472) >> 16;
        }
        public  Color getDominantColor(Bitmap bmp)
        {
            //Used for tally
            int r = 0;
            int g = 0;
            int b = 0;

            int total = 0;

            for (int x = 0; x < bmp.Width; x++)
            {
                for (int y = 0; y < bmp.Height; y++)
                {
                    Color clr = bmp.GetPixel(x, y);

                    r += clr.R;
                    g += clr.G;
                    b += clr.B;

                    total++;
                }
            }

            //Calculate average
            r /= total;
            g /= total;
            b /= total;
          Response.Write("r:"+r);
          Response.Write("g:" + g);
          Response.Write("b:" + b);
            return Color.FromArgb(r, g, b);
        }

        /// <summary>
        /// 灰度转换,逐点方式
        /// </summary>
        public void GrayByPixels()
        {
            int totalNum = 0;
            int totalGrayPix = 0;
            
            for (int i = 0; i < bmpobj.Height; i++)
            {
                for (int j = 0; j < bmpobj.Width; j++)
                {
                    int tmpValue = GetGrayNumColor(bmpobj.GetPixel(j, i));
                    totalNum += 1;
                    totalGrayPix += tmpValue;

                    //反转
                    //bmpobj.SetPixel(j, i, Color.FromArgb(tmpValue, tmpValue, tmpValue));
                }
            }

            //判断是否是黑色图片：0=全黑，255=全白；这种方法不是很准的，效率也不是很高
            //取各个像素的平均值
            int avgGray = totalGrayPix / totalNum;
            if (avgGray < 50)
            {
               Response.Write("像素灰度平均值：" + avgGray.ToString() + ",认为是黑色或接近黑色");
            }
            else
            {
                Response.Write("彩色图像");
            }

        }
      
        /// <summary>
        /// 判断图片否是是彩色
        /// </summary>
        /// <param name="filename">图片文件路径</param>
        /// <returns></returns>
        public bool isBlackWhite(string filename)
        {
            Color c = new Color();

            using (Bitmap bmp = new Bitmap(filename))
            {
                //历遍图片的像素点
                for (int y = 0; y < bmp.Height; y++)
                {
                    for (int x = 0; x < bmp.Width; x++)
                    {
                        c = bmp.GetPixel(x, y);

                        //判断像素点的色偏差值Diff
                        if (GetRGBDiff(c.R, c.G, c.B) > 50)
                        {
                            return false;
                        }
                    }
                }

                return true;
            }
        }
        public int GetRGBDiff(int r, int g, int b)
        {
            //略，很单简的，就是取r-g,r-b,g-b绝对值的最大值。
            r = Math.Abs(r);
            g = Math.Abs(g);
            b = Math.Abs(b);
           return Math.Max(Math.Max(r, g), b);
        }

        //public  List<Color> CountImageColors(string fileName)
        //{
           
        //    List<Color> colors = new List<Color>();
        //    Bitmap bmp = null;

        //    if (File.Exists(fileName))
        //    {
        //        try
        //        {
        //            bmp = new Bitmap(fileName);
        //            if (bmp != null)
        //            {
        //                for (int y = 0; y < bmp.Size.Height; y++)
        //                {
        //                    for (int x = 0; x < bmp.Size.Width; x++)
        //                    {
        //                        colors.Add(bmp.GetPixel(x, y));
        //                    }
        //                }
        //                return colors;
        //            }
        //        }
        //        catch(Exception ex)
        //        {
        //            Response.Write(ex.ToString());
        //        }
        //        finally
        //        {
        //            colors.Clear();
        //            bmp.Dispose();
        //        }
        //    }
        //    return colors;
        //}

        public int CountImageColors(string fileName)
        {
           int count = 0;
            HashSet<Color> colors = new HashSet<Color>();
            Bitmap bmp = null;

            if (File.Exists(fileName))
            {
                try
                {
                    bmp = new Bitmap(fileName);
                    if (bmp != null)
                    {
                        for (int y = 0; y < bmp.Size.Height; y++)
                        {
                            for (int x = 0; x < bmp.Size.Width; x++)
                            {
                                colors.Add(bmp.GetPixel(x, y));
                            }
                        }
                      
                    }
                    count = colors.Count;
                }
                catch
                {
                    throw;
                }
                finally
                {
                    colors.Clear();
                    bmp.Dispose();
                }
            }
            return count;
        }
    }
    /// <summary>
    /// 去"重复"时候的比较器(只要ProductNo相同，即认为是相同记录)
    /// </summary>
    class ProductNoComparer : IEqualityComparer<Color>
    {
        public bool Equals(Color p1, Color p2)
        {
            if (p1 == null)
                return p2 == null;
            return p1.Name == p2.Name;
        }

        public int GetHashCode(Color p)
        {
            if (p == null)
                return 0;
            return p.Name.GetHashCode();
        }
    }
}